package com.tools.cleanmaster.ui.notification.item;

import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.text.Html;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.scenenew.SceneConstants;
import com.tools.cleanmaster.scenenew.SceneType;
import com.tools.cleanmaster.store.SceneSharedPrefManager;
import com.tools.cleanmaster.ui.activity.LandingPageGuideActivity;
import com.tools.cleanmaster.ui.notification.NotificationBaseItem;
import com.tools.cleanmaster.ui.notification.UIBuilder;
import com.tools.cleanmaster.ui.notification.UIType;
import com.tools.cleanmaster.utils.FeatureConfig;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 总内存占用过高
 */
public class TotalMemoryUsageItem extends NotificationBaseItem {
    private static final String JSON_KEY_SWITCH = "switch";
    private static final String JSON_KEY_INTERVAL = "interval";
    private static final String JSON_KEY_PERCENT = "percent";
    private static final boolean DEFAULT_SWITCH = true;
    private static final int DEFAULT_INTERVAL_HOUR = 24;
    private static final int DEFAULT_MIN_PERCENT = 70;

    private boolean mSwitch = DEFAULT_SWITCH;     //开关，默认开
    private int mIntervalHour = DEFAULT_INTERVAL_HOUR;    //时间间隔，单位h,默认1天
    private int mMinPercent = DEFAULT_MIN_PERCENT;      // 默认最低触发比例 (*100)
    private int mPercent;

    public TotalMemoryUsageItem(int memPercent) {
        super();
        String jsonBody = SceneSharedPrefManager.getSingleSceneConfigData(getSceneType());
        try {
            JSONObject jsonObject = new JSONObject(jsonBody);
            mSwitch = jsonObject.optBoolean(JSON_KEY_SWITCH, DEFAULT_SWITCH);
            mIntervalHour = jsonObject.optInt(JSON_KEY_INTERVAL, DEFAULT_INTERVAL_HOUR);
            mMinPercent = jsonObject.optInt(JSON_KEY_PERCENT, DEFAULT_MIN_PERCENT);
        } catch (JSONException e) {
            if (FeatureConfig.DEBUG_LOG) {
                e.printStackTrace();
            }
        }
        mPercent = memPercent;
    }

    @Override
    public boolean isSceneValid() {
        if (!mSwitch) {
            if (FeatureConfig.DEBUG_LOG) log("开关关闭，无法展示");
            return false;
        }

        boolean isValid = mPercent > mMinPercent;
        if (FeatureConfig.DEBUG_LOG)
            log("应用内存占用百分比：" + mPercent + ",配置百分比：" + mMinPercent + ",能否展示：" + isValid);
        return isValid;
    }

    @Override
    public Notification prepareNotification() {
        Context ctx = CleanApplication.getInstance();
        Intent intent = new Intent(ctx, LandingPageGuideActivity.class);
        intent.putExtra(SceneConstants.EXTRA_FROM_KEY, SceneConstants.EXTRA_FROM_VALUE_STATUS_BAR);
        intent.putExtra(SCENE_TYPE_EXTRA_FLAG, SceneType.BG_MEM_OVERLOAD.key);

        UIParam param = new UIParam();
        param.viewType = UIType.TITLE_WITH_CONTENT_GREEN_BTN;
        param.tickerText = Html.fromHtml(String.format(ctx.getString(R.string.total_memory_tickertext), String.valueOf(mPercent)));
        param.title = Html.fromHtml(String.format(ctx.getString(R.string.total_memory_tickertext), String.valueOf(mPercent)));
        param.sceneType = getSceneType();
        param.content = Html.fromHtml(ctx.getResources().getString(R.string.total_memory_content));
        param.btnText = ctx.getResources().getString(R.string.battery_low_btn);
        param.leftIconResId = R.drawable.ic_scene_ramlow;
        param.leftText = String.valueOf(mPercent);
        param.tickerIcon = R.drawable.ic_notify_scene_ramlow;
        param.iconTextColor = ctx.getResources().getColor(
                R.color.notification_icon_text_color_orange);
        param.iconText = String.format(ctx.getResources().getConfiguration().locale, "%1$d%%", mPercent);
        param.contentIntent = intent;
        UIBuilder builder = new UIBuilder(param);
        return builder.build();
    }

    @Override
    public SceneType getSceneType() {
        return SceneType.BG_MEM_OVERLOAD;
    }

    @Override
    public boolean isTimeValid() {
        long lastShow = SceneSharedPrefManager.getLastSceneShowTime(getSceneType());
        long now = System.currentTimeMillis();
        if (now - lastShow < mIntervalHour * SceneConstants.HOUR_MS) {
            if (FeatureConfig.DEBUG_LOG) log("距离上次展示小于 " + mIntervalHour + " 小时，无法展示");
            return false;
        }
        return true;
    }

    public boolean isFirstIn() {
        return SceneSharedPrefManager.isFirstInScene(getSceneType().key);
    }
}
